local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

----------------------------------------------------------------------------------
-- 
--      NOTE:
--      
--      Code outside of listener functions (below) will only be executed once,
--      unless storyboard.removeScene() is called.
-- 
---------------------------------------------------------------------------------


-- local forward references should go here --


---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- Called when the scene's view does not exist:
function scene:createScene(sceneEvent)
        local group = self.view

        -----------------------------------------------------------------------------

        --      CREATE display objects and add them to 'group' here.
        --      Example use-case: Restore 'group' from previously saved state.

        -----------------------------------------------------------------------------
		
		local pauseBG1 = display.newImageRect("pauseBg1.png",320,480)
		pauseBG1.x = display.contentWidth/2
		pauseBG1.y = display.contentHeight/2
		
		local pauseBG = display.newImageRect("pauseBg2.png",284,392)
		pauseBG.x = display.contentWidth/2
		pauseBG.y = display.contentHeight/2
		
		local menuButtonUp = display.newImage("pMenuBntUp.png")
		local menuButtonDown = display.newImage("pMenuBntDown.png")
		
		local resumeButtonUp = display.newImage("pResumeBntUp.png")
		local resumeButtonDown = display.newImage("pResumeBntDown.png")
		
		local restartButtonUp = display.newImage("pReloadBntUp.png")
		local restartButtonDown = display.newImage("pReloadBntDown.png")
		

		
		menuButtonDown.isVisible = false
		
		menuButtonUp.x = display.contentWidth/2
		menuButtonUp.y = display.contentHeight/2 - 70
		
		menuButtonDown.x = display.contentWidth/2
		menuButtonDown.y = display.contentHeight/2 - 70
	
		
		resumeButtonDown.isVisible = false
		
		resumeButtonUp.x = display.contentWidth/2
		resumeButtonUp.y = display.contentHeight/2
		
		resumeButtonDown.x = display.contentWidth/2
		resumeButtonDown.y = display.contentHeight/2

		
		restartButtonDown.isVisible = false
		
		restartButtonUp.x = display.contentWidth/2
		restartButtonUp.y = display.contentHeight/2 + 70
		
		restartButtonDown.x = display.contentWidth/2
		restartButtonDown.y = display.contentHeight/2 + 70
	
		
		group:insert(pauseBG1)
		group:insert(pauseBG)
		group:insert(menuButtonUp)
		group:insert(menuButtonDown)
		group:insert(resumeButtonUp)
		group:insert(resumeButtonDown)
		group:insert(restartButtonUp)
		group:insert(restartButtonDown)

		
		local function menuBntTouch(touchEvent) 						
			local function gotoMenu()
				print("Goto Menu")				
				storyboard.purgeScene("GamePlay")				
				storyboard.hideOverlay()				
				storyboard.gotoScene("Menu",{time=200,effect="flipFadeOutIn"})
			end						
			if (touchEvent.phase == "began") then
				menuButtonDown.isVisible = true
			else
				gotoMenu()
				menuButtonDown.isVisible = false
			end			
			return true
		end 
		menuButtonUp:addEventListener( "touch", menuBntTouch )
		
		local function resumeBntTouch(touchEvent)
			local function resumeGame()
				print("Resume")								
				storyboard.hideOverlay()
				sceneEvent.params.controler.resumeGame()
			end						
			if (touchEvent.phase == "began") then
				resumeButtonDown.isVisible = true
			else
				resumeGame()
				resumeButtonDown.isVisible = false
			end			
			return true
		end 
		resumeButtonUp:addEventListener( "touch", resumeBntTouch )
		
		
		local function restartBntTouch(touchEvent)
			local function restartGame()
				print("Restart")								
				storyboard.hideOverlay()
				--sceneEvent.params.controler.resumeGame()
				storyboard.purgeScene("GamePlay")
				storyboard.gotoScene("GamePlay",{time=500,effect="zoomInOut" })
			end						
			if (touchEvent.phase == "began") then
				restartButtonDown.isVisible = true
			else
				restartGame()
				restartButtonDown.isVisible = false
			end			
			return true
		end 
		restartButtonUp:addEventListener( "touch", restartBntTouch )
		
		
end


-- Called BEFORE scene has moved onscreen:
function scene:willEnterScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end


-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. start timers, load audio, start listeners, etc.)

        -----------------------------------------------------------------------------

end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

        -----------------------------------------------------------------------------

end


-- Called AFTER scene has finished moving offscreen:
function scene:didExitScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. remove listeners, widgets, save state, etc.)

        -----------------------------------------------------------------------------

end


-- Called if/when overlay scene is displayed via storyboard.showOverlay()
function scene:overlayBegan( event )
        local group = self.view
        local overlay_name = event.sceneName  -- name of the overlay scene

        -----------------------------------------------------------------------------

        --      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end


-- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()
function scene:overlayEnded( event )
        local group = self.view
        local overlay_name = event.sceneName  -- name of the overlay scene

        -----------------------------------------------------------------------------

        --      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end



---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "willEnterScene" event is dispatched before scene transition begins
scene:addEventListener( "willEnterScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "didExitScene" event is dispatched after scene has finished transitioning out
scene:addEventListener( "didExitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-- "overlayBegan" event is dispatched when an overlay scene is shown
scene:addEventListener( "overlayBegan", scene )

-- "overlayEnded" event is dispatched when an overlay scene is hidden/removed
scene:addEventListener( "overlayEnded", scene )

---------------------------------------------------------------------------------

return scene